package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.PaasKitPrintService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.print.pojo.pojo.dto.PaasKitPrintListDTO;
import com.xbongbong.pro.print.pojo.pojo.vo.PaasKitPrintListVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 套打接口
 *
 * @author hongxiao
 * @date 2021-01-18 15:46
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/kitPrint")
public class PaasKitPrintController {

    @Resource
    private PaasKitPrintService paasKitPrintService;

    /**
     * 获取默认套打模板列表
     *
     * @param
     * @throws
     * @return
     * @author hongxiao
     * @date 2021-01-18 16:02
     * @since
     * @version
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid PaasKitPrintListDTO paasKitPrintListDTO, BindingResult br) throws Exception {
        XbbResponse<PaasKitPrintListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaasKitPrintListVO paasKitPrintListVO = paasKitPrintService.list(paasKitPrintListDTO);
                response = new XbbResponse<>(paasKitPrintListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


}
